package cn.edu.zstu.overlappingdisappear.api;

import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

import cn.edu.zstu.overlappingdisappear.base.MyApplication;
import cn.edu.zstu.overlappingdisappear.utils.NetUtils;

/**
 * @Author ZhuMingWei(986990501@qq.com)
 * @Date on 2019/12/2
 * @Version v1.0.0
 * @Company TODO
 * @Describle TODO
 */

public class UserApi {

    private static final String TAG = "STR_POST";

    private static final String OFFICIAL_SERVER = "http://qkty2q.natappfree.cc/overlappingdisappear";
    public static String BASE_SERVER = OFFICIAL_SERVER;

    //1. 注册接口
    private static final String REGISTER_URL = "/signUp/";
    //2. 登录接口
    private static final String LOGIN_URL = "/signIn/";
    //3.查询用户名是否存在
    private static final String USEREXIST_URL = "/searchUser/";
    //4.房间创建
    private static final String MATCH_URL = "/createRoom/";
    //5.查询房间状态
    private static final String ROOMSTATE_URL = "/searchRoomState/";
    //6.提交游戏结果
    private static final String COMMIT_RESULT_URL = "/resultCommit/";



    public static void register(final String username, final String password, final String nickname, VolleyStrHandler handler) {

        if (!NetUtils.isConnected(MyApplication.getInstance())) {
            Toast.makeText(MyApplication.getInstance(), "无网络连接", Toast.LENGTH_SHORT).show();
            return;
        }
        cancelAll();//防止出现多次重复请求

        StringRequest strReq = new StringRequest(Request.Method.POST, OFFICIAL_SERVER + REGISTER_URL, handler, handler) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("username", username);
                params.put("password", password);
                params.put("nickname", nickname);
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(strReq, TAG);
    }

    public static void usernameExist(final String username , VolleyStrHandler handler) {

        if (!NetUtils.isConnected(MyApplication.getInstance())) {
            Toast.makeText(MyApplication.getInstance(), "无网络连接", Toast.LENGTH_SHORT).show();
            return;
        }
        cancelAll();//防止出现多次重复请求

        StringRequest strReq = new StringRequest(Request.Method.POST, OFFICIAL_SERVER + USEREXIST_URL, handler, handler) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("username", username);
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(strReq, TAG);
    }

    public static void login(final String username, final String password, VolleyStrHandler handler) {

        if (!NetUtils.isConnected(MyApplication.getInstance())) {
            Toast.makeText(MyApplication.getInstance(), "无网络连接", Toast.LENGTH_SHORT).show();
            return;
        }
        cancelAll();//防止出现多次重复请求

        StringRequest strReq = new StringRequest(Request.Method.POST, OFFICIAL_SERVER + LOGIN_URL, handler, handler) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("username", username);
                params.put("password", password);
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(strReq, TAG);
    }

    public static void match(final String username, VolleyStrHandler handler) {

        if (!NetUtils.isConnected(MyApplication.getInstance())) {
            Toast.makeText(MyApplication.getInstance(), "无网络连接", Toast.LENGTH_SHORT).show();
            return;
        }
        cancelAll();//防止出现多次重复请求

        StringRequest strReq = new StringRequest(Request.Method.POST, OFFICIAL_SERVER + MATCH_URL, handler, handler) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("username", username);
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(strReq, TAG);
    }

    public static void searchRoomState(final String roomid, VolleyStrHandler handler) {

        if (!NetUtils.isConnected(MyApplication.getInstance())) {
            Toast.makeText(MyApplication.getInstance(), "无网络连接", Toast.LENGTH_SHORT).show();
            return;
        }
        cancelAll();//防止出现多次重复请求

        StringRequest strReq = new StringRequest(Request.Method.POST, OFFICIAL_SERVER + ROOMSTATE_URL, handler, handler) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("roomid", roomid);
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(strReq, TAG);
    }

    public static void commitResult(final String username,final String roomid,final int finishTime, VolleyStrHandler handler) {

        if (!NetUtils.isConnected(MyApplication.getInstance())) {
            Toast.makeText(MyApplication.getInstance(), "无网络连接", Toast.LENGTH_SHORT).show();
            return;
        }
        cancelAll();//防止出现多次重复请求

        StringRequest strReq = new StringRequest(Request.Method.POST, OFFICIAL_SERVER + COMMIT_RESULT_URL, handler, handler) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("username", username);
                params.put("roomid", roomid);
                params.put("finishtime", finishTime+"");
                return params;
            }
        };
        MyApplication.getInstance().addToRequestQueue(strReq, TAG);
    }

    public interface VolleyStrHandler extends Response.Listener<String>, Response.ErrorListener {
        @Override
        void onErrorResponse(VolleyError error);

        @Override
        void onResponse(String responseStr);
    }

    public static void cancelAll() {
        MyApplication.getInstance().getRequestQueue().cancelAll(TAG);
    }
}
